<?php 

$max = 2;
$workers = [];

//任务列表
$list = [1,2,3,4,5,6,7,8];

$chan = new Swoole\Channel(1024 * 256);
foreach($list as $item){
	$chan->push($item);
}

function callback(swoole_process $worker,&$chan){
	$v = $chan->pop();
	var_dump($v);
	sleep(3);
	var_dump($worker);
	var_dump($chan->stats());
}

$g = [];
//设置指定个任务,从任务队列中取出一个进行处理
$i = 0;
while(true){
	if($i>=$max){
		break;
	}else if(empty($list)){
		break;
	}
	$i ++;
    $process = new \swoole_process(function ($worker) use ($chan){
    	callback($worker,$chan);
    },FALSE);
    $pid = $process->start();
    $workers[$pid] = $process;
}

//捕获进程退出事件
\swoole_process::signal(SIGCHLD,function ($signo) use (&$workers,&$chan){
    while($ret = \swoole_process::wait(FALSE)){
        $pid     = $ret['pid'];
        unset($workers[$pid]);

        $status = $chan->stats();
        if($status['queue_num'] == 0){
        	exit;
        }
	    $new_process = new \swoole_process(function ($worker) use (&$chan){
	    	callback($worker,$chan);
	    },FALSE);
        $new_pid = $new_process->start();
        $workers[$new_pid] = $new_process;
    }
});